<template>
	<view class="padding-30">
		<view class="padding-30 bg-fff border-radius20 margin-bottom30">
			<view class="flex-between padding-bottom30 f-divider1 margin-bottom30">
				<view class="fontSize-15 color-000 margin-right20">现病史</view>
				<view class="flex-alignItems-center" @click="diseaseShow = true">
					<view class="fontSize-15 color-000 margin-right10" v-if="diseaseName">{{diseaseName}}</view>
					<view class="fontSize-15 color-999 margin-right10" v-else>请选择</view>
					<u-icon name="arrow-right" color="#666666" size="18"></u-icon>
				</view>
			</view>
			<view class="flex-between padding-bottom30 f-divider1 margin-bottom30">
				<view class="fontSize-15 color-000 margin-right20">病史时间</view>
				<view class="flex-alignItems-center" @click="diseaseDateShow = true">
					<view class="fontSize-15 color-000 margin-right10" v-if="diseaseDateName">{{diseaseDateName}}</view>
					<view class="fontSize-15 color-999 margin-right10" v-else>请选择</view>
					<u-icon name="arrow-right" color="#666666" size="18"></u-icon>
				</view>
			</view>
		</view>

		<view class="padding-30 bg-fff border-radius20 margin-bottom30">
			<view class="fontSize-15 color-000 margin-bottom30">病症描述</view>
			<u--textarea v-model="describe" placeholder="请输入内容" height="100"></u--textarea>
		</view>

		<!-- 底部操作栏 -->
		<view style="height: 200rpx;"></view>
		<view class="position-fixed-bottom padding-30 bg-fff">
			<view class="btn50-0165FD-15ffffff flex-center" @click="SubmitEvent">
				保存
			</view>
		</view>

		<!-- 现病史弹窗 -->
		<u-picker :show="diseaseShow" :columns="[diseaseColumns,diseaseColumns1]" keyName="name" @change="diseaseChange"
		@cancel="diseaseCancel" @confirm="diseaseConfirm"></u-picker>
		
		<!-- 病史时间 -->
		<u-datetime-picker  :maxDate="Number(new Date())" :show="diseaseDateShow" v-model="diseaseDateValue" mode="datetime" 
		@cancel="diseaseDateCancel" @confirm="diseaseDateConfirm"></u-datetime-picker>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 现病史
				diseaseList:[], // 现病史数据
				diseaseColumns: [], //现病史 第一列
				diseaseColumns1: [], //现病史 第二列
				diseaseShow: false,
				diseaseName:'',
				// 病史时间
				diseaseDateShow:false,
				diseaseDateValue:Number(new Date()),
				diseaseDateName:'',
				// 病史描述
				describe:'',
				sick_id:'',
				form:{
					sick_id:'',
					tags:'',
					describe:'',
					tag_date:''
				},
				code:'',
				disease_id:''
			}
		},
		onLoad(e) {
			if(e.code){
				this.sick_id = uni.getStorageSync('patlient-item').id
				this.disease_id = e.id
				this.code = e.code
				this.getDescribeDetail()
				this.getConfig()
			}else{
				if(uni.getStorageSync('patlient-item')){
					this.sick_id = uni.getStorageSync('patlient-item').id
					this.getConfig()
				}
			}
		},
		methods: {
			// 获取现病史详情
			async getDescribeDetail(){
				let res = await this.$http.post(this.$api.disease_detail,{sick_id:this.sick_id},{token:true})
				if(res.code ==1){
					this.form.sick_id = res.data.sick_id
					this.form.tags = res.data.tags
					this.diseaseName = res.data.tags
					this.form.describe = res.data.describe
					this.describe = res.data.describe
					this.form.tag_date = res.data.tag_date
					this.diseaseDateName = res.data.tag_date
				}
			},
			// 提交病史
			async SubmitEvent(){
				if(!this.diseaseName) return this.$tools.toast('请选择现病史')
				if(!this.diseaseDateName) return this.$tools.toast('请选择病史时间')
				if(!this.describe) return this.$tools.toast('请添加病史描述')
				this.form.sick_id = this.sick_id
				this.form.tags = this.diseaseName
				this.form.describe = this.describe
				this.form.tag_date = this.diseaseDateName
				if(this.code == '现病史编辑'){
					this.form.id = this.disease_id
					let res = await this.$http.post(this.$api.disease_edit,this.form,{token:true})
					if(res.code ==1){
						this.$tools.toast('修改成功')
						this.$tools.navigateBack(1,200)
					}
				}else{
					let res = await this.$http.post(this.$api.disease_add,this.form,{token:true})
					if(res.code ==1){
						this.$tools.toast('添加成功')
						this.$tools.navigateBack(1,200)
					}
				}
			},
			// 确认时间
			diseaseDateConfirm(e){
				this.diseaseDateName = this.$T.timeFormat(e.value)
				this.diseaseDateShow = false
			},
			// 取消时间
			diseaseDateCancel(){
				this.diseaseDateName = ''
				this.diseaseDateShow = false
			},
			// 确认现病史
			diseaseConfirm(e){
				console.log(e)
				this.diseaseName = e.value[0].name+','+e.value[1]
				this.diseaseShow = false
			},
			// 取消现病史
			diseaseCancel(){
				this.diseaseName = ''
				this.diseaseShow = false
			},
			// 现病史切换
			diseaseChange(e){
				if(e.columnIndex == 0){
					this.diseaseList.forEach(item=>{
						if(e.value[0].id == item.id ){
							this.diseaseColumns1 = item.name
						}
					})
				}
			},
			// 获取配置信息
			async getConfig() {
				let res = await this.$http.post(this.$api.config, {}, {
					token: true
				})
				let arr = []
				let arr1 = []
				if (res.code == 1) {
					Object.keys(res.data.病症).forEach(key => {
						// 执行循环体代码
						arr.push({
							id: key,
							name: res.data.病症[key]
						})
					});
				}
				if (arr.length > 0) {
					this.diseaseList = arr
					arr1 = arr.map(item => {
						return {
							id: item.id,
							name: item.id
						}
					})
					this.diseaseColumns = arr1
					this.diseaseColumns1 = arr[0].name
				}
			}
		}
	}
</script>

<style lang="scss" scoped>

</style>